
import * as types from '../mutation-types'


const state = {
    deptList: [],        // 查询组织架构
    roleList: [],        // 查询所有角色
    orgList: [],         // 架构列表
    changeOrgValue: 0    // 更新组织架构数据
}

const mutations = {
    [types.GET_DEPT_LIST](state) {
        api.queryDept().then(data => {
            state.deptList = data.tree
        }).catch(res => {
            console.log(res)
        })
    },
    [types.GET_ROLE_LIST](state) {
        api.queryRole().then(data => {
            state.roleList = data.tree
        }).catch(res => {
            console.log(res)
        })
    },
    [types.GET_ORG_LIST](state, obj) {
        api.queryOrgList(obj.data).then(data => {
            if (typeof obj.callback === 'function') {
                obj.callback(data);
            }
        }).catch(res => {
            console.log(res)
        })
    }
}

const getters = {
    // 组织架构列表
    getDeptList(state) {
        return state.deptList
    },
    // 角色列表
    getRoleList(state) {
        return state.roleList
    },
    // 架构列表
    getOrgList(state) {
        return state.orgList
    }
}

const actions = {
    getDeptList: ({ commit }) => commit(types.GET_DEPT_LIST),
    getRoleList: ({ commit }) => commit(types.GET_ROLE_LIST),
    getOrgList: ({ commit }, param) => commit(types.GET_ORG_LIST, param)
}

export default {
    state,
    getters,
    mutations,
    actions
}